//Copyright (C) 2009 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

package org.workplicity.cia.ui.android;

import org.workplicity.cia.ui.android.elogfragments.CatchEditFragment;
import org.workplicity.cia.ui.android.elogfragments.CommentEditFragment;
import org.workplicity.cia.ui.android.elogfragments.HandoffEditFragment;
import org.workplicity.cia.ui.android.elogfragments.LinkEditFragment;
import org.workplicity.cia.ui.android.elogfragments.LogEditFragment;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;

/**
 * 
 * @author brian.gormanly
 *
 */

public class ElogEditActivity extends Activity {

	private View mActionBarView;
	private int mThemeId = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 
		if(savedInstanceState != null && savedInstanceState.getInt("theme", -1) != -1) {
			 mThemeId = savedInstanceState.getInt("theme");
			 this.setTheme(mThemeId);
	    }
		else {
			 // default
			 this.setTheme(R.style.AppTheme_Light);
		}
		
		setContentView(R.layout.elogedit);
	        
        ActionBar bar = getActionBar();
        
        // get the resourcess available
        Resources res = getResources();

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        // Get the string array that has the tabs / names
        String[] tabNames = res.getStringArray(R.array.eltab_array);
        
        ActionBar.Tab tabA = bar.newTab().setText(tabNames[0]);
        ActionBar.Tab tabB = bar.newTab().setText(tabNames[1]);
        ActionBar.Tab tabC = bar.newTab().setText(tabNames[2]);
        ActionBar.Tab tabD = bar.newTab().setText(tabNames[3]);
        ActionBar.Tab tabE = bar.newTab().setText(tabNames[4]);

        Fragment logEditFrag = new LogEditFragment();
        Fragment handoffEditFrag = new HandoffEditFragment();
        Fragment linkEditFrag = new LinkEditFragment();
        Fragment commentEditFrag = new CommentEditFragment();
        Fragment catchEditFrag = new CatchEditFragment();
        
        tabA.setTabListener(new MyTabListener(logEditFrag));
        tabB.setTabListener(new MyTabListener(handoffEditFrag));
        tabC.setTabListener(new MyTabListener(linkEditFrag));
        tabD.setTabListener(new MyTabListener(commentEditFrag));
        tabE.setTabListener(new MyTabListener(catchEditFrag));
        

        mActionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom, null);

        bar.setCustomView(mActionBarView);
        bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowHomeEnabled(true);
        
        bar.addTab(tabA);
        bar.addTab(tabB);
        bar.addTab(tabC);
        bar.addTab(tabD);
        bar.addTab(tabE);

	 }
	 
	 
	 protected class MyTabListener implements ActionBar.TabListener {
	        private Fragment myFragment;

	        // Called to create an instance of the listener when adding a new tab
	        public MyTabListener(Fragment fragment) {
	            myFragment = fragment;
	        }

	        @Override
			public void onTabSelected(Tab tab, FragmentTransaction ft) {
	            ft.add(R.id.logfragments, myFragment, null);
	        }

	        @Override
			public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	            ft.remove(myFragment);
	        }

	        @Override
			public void onTabReselected(Tab tab, FragmentTransaction ft) {
	            // do nothing
	        }

	    }

}
